package com.example.demo.aspect;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.demo.domain.SgBStoTobOutNotices;
import com.example.demo.mapper.SgBStoTobOutNoticesMapper;
import com.example.demo.service.SgBStoTobOutNoticesService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: kangyang
 * @Description: TODO
 * @DateTime: 2024/10/11 17:17
 **/
@Aspect
@Component
public class ApiLogAspect {

    @Autowired
    SgBStoTobOutNoticesMapper sgBStoTobOutNoticesMapper;

    @Around("execution(public * com.example.demo.controller..*.*(..))")
    public Object recordLog(ProceedingJoinPoint point) throws Throwable {
        System.out.println("=============before=============");
        Object proceed = point.proceed();
        System.out.println("=============after=============");
        return proceed;
    }

    // 后置通知不能有ProceedingJoinPoint
//    @After("execution(* com.example.demo.service.SgBStoTobOutNoticesService.queryByBillNo(..))")
//    public Object serviceAspect(ProceedingJoinPoint point) throws Throwable {
//        SgBStoTobOutNoticesService target = (SgBStoTobOutNoticesService) point.getTarget();
//        System.out.println("=============serviceAspect=============");
//        return point.proceed();
//    }

//    @AfterReturning(pointcut = "execution(* com.example.demo.service.impl.SgBStoTobOutNoticesServiceImpl.updateNotices(..))", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
//        int testTransaction = sgBStoTobOutNoticesMapper.update(new SgBStoTobOutNotices(), new LambdaUpdateWrapper<SgBStoTobOutNotices>().eq(SgBStoTobOutNotices::getId, 11L).set(SgBStoTobOutNotices::getRemark, "testTransaction aspect"));
        System.out.println("Method executed successfully.");
        System.out.println("Return value: " + result);
    }

    @AfterReturning(pointcut = "execution(* com.example.demo.service.impl.SgBStoTobOutNoticesServiceImpl.updateNotices(..))", returning = "result")
    public void afterReturning1(JoinPoint joinPoint, Object result) {
        System.out.println("Method executed successfully.1");
        System.out.println("Return value: " + result);
    }

    @AfterReturning(pointcut = "execution(* com.example.demo.service.impl.SgBStoTobOutNoticesServiceImpl.updateNotices(..))", returning = "result")
    public void afterReturning2(JoinPoint joinPoint, Object result) {
        System.out.println("Method executed successfully.2");
        System.out.println("Return value: " + result);
    }
}
